<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/23
 * Time: 15:49
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Person extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            if(isset($keyword) && $keyword!="")
            {
                $map['person_name'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('person')->alias('a')
                ->join('up_school us','us.hot_id = a.lp_id','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('person')->alias('a')
                ->page($NowPage,$limits)
                ->join('up_school us','us.hot_id = a.lp_id','left')
                ->field('a.*,us.hot_keyword')
                ->where($map)
                ->order('a.person_sort asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function sort(){
        $param = input('');
        $res = Db::table('up_person')->where('person_id', $param['id'])->setField('person_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $music_type_name =input('person_name');
            $music_type_img= input('person_img');
            $person_intro= input('person_intro');
            $person_troupe= input('person_troupe');
            $video_lp= input('video_lp');
            $data['person_name'] = $music_type_name;
            $data['person_img'] = $music_type_img;
            $data['person_time'] = date('Y-m-d H:i:s',time());
            $data['person_intro'] = $person_intro;
            $data['lp_id'] = $video_lp;
            $data['person_troupe'] = $person_troupe;
            $res = Db::table('up_person')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info3 = Db::table('up_school')->select();
        $info4 = Db::table('up_troupe_type')->select();
        $this->assign('info3',$info3);
        $this->assign('info4',$info4);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $music_type_name =input('person_name');
            $music_type_img= input('person_img');
            $person_intro= input('person_intro');
            $person_troupe= input('person_troupe');
            $data['person_troupe'] = $person_troupe;
            $data['person_name'] = $music_type_name;
            $data['person_intro'] = $person_intro;
            $video_lp= input('video_lp');
            $data['lp_id'] = $video_lp;
            if(!empty($music_type_img)){
                $data['person_img'] = $music_type_img;
            }
            $res = Db::table('up_person')->where('person_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('id');
        $info= Db::table('up_person')->where('person_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        $info3 = Db::table('up_school')->select();
        $info4 = Db::table('up_troupe_type')->select();
        $this->assign('info4',$info4);
        $this->assign('info3',$info3);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *
     */
    public function up(){
        $task_id = input('param.id');
        $count = Db::table('up_person')
            ->where('is_index',1)
            ->count();
        if($count>=1){
            return json(['code'=>100,'msg'=>'最多指定1个名家，请先下架其他名家在指定']);
        }
        $res = Db::name('person')->where('person_id',$task_id)->setField('is_index',1);
        if($res){
            return json(['code'=>200,'msg'=>'指定成功']);
        }else{
            return json(['code'=>100,'msg'=>'指定失败']);
        }
    }

    /***
     * @return \think\response\Json
     *
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('person')->where('person_id',$task_id)->setField('is_index',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('person')->where('person_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function isStatus(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('person')->where('person_id',$task_id)->setField('person_status',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}